<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="姓名">
              <el-input v-model="params.username" placeholder="请输入姓名" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="手机号">
              <el-input v-model="params.mobile" placeholder="请输入手机号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="答题情况">
              <el-select v-model="params.status">
                <el-option
                  v-for="item in StatusOption"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="开始答题时间">
              <el-date-picker
                type="daterange"
                value-format="yyyy-MM-dd "
                range-separator="-"
                start-placeholder="起始时间"
                end-placeholder="截止时间"
                v-model="time1"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="提交答题时间">
              <el-date-picker
                type="daterange"
                value-format="yyyy-MM-dd "
                range-separator="-"
                start-placeholder="起始时间"
                end-placeholder="截止时间"
                v-model="time2"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="20">
            <el-button type="primary" @click="searchFn">查询</el-button>
            <!-- <el-button type="primary" @click="excelEvent">导出</el-button> -->
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- table -->
    <div class="table" v-loading="tableLoading">
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%">
        <el-table-column fixed type="index" align="center" label="编号" width="55"></el-table-column>
        <el-table-column prop="name" align="center" label="姓名"></el-table-column>
        <el-table-column prop="mobile" align="center" label="手机号">
          <template slot-scope="scope">
            <span>{{scope.row.mobile | phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="beauty_place" align="center" label="想要变美部位"></el-table-column>
        <el-table-column prop="status" align="center" label="答题情况">
          <template slot-scope="scope">
            <span>{{scope.row.status|withdrawStatusFilter}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="answer_number" align="center" label="参与答题数"></el-table-column>
        <el-table-column prop="correct_number" align="center" label="答对题目数"></el-table-column>
        <el-table-column prop="created_at" align="center" label="开始答题时间"></el-table-column>
        <el-table-column prop="ended_at" align="center" label="提交答题时间"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <div>
              <el-button type="text" @click="toRoutes('answerManageDetail',scope.row.id)">查看</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagesCon">
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="params.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { getList, excelExport } from "@/api/answerManageIndex";
import { exportfile } from "@/api/exporfile";
import axios from "axios";

export default {
  name: "answerManageIndex",
  data() {
    return {
      permissionJson: JSON.parse(localStorage.getItem("permissionJson")),
      // loading层
      tableLoading: false,
      //提交查询数据
      params: {
        page: 1,
        pagesize: 10,
        username: "",
        mobile: "",
        status: "",
        started_at: "",
        ended_at: "",
      },
      tableData: [],
      total: 0,
      time1: [],
      time2: [],

      //答题状态下拉框
      StatusOption: [
        { id: "", name: "全部" },
        { id: "0", name: "未完成" },
        { id: "1", name: "已完成" },
      ],
    };
  },
  inject: ["reload"],
  //过滤
  filters: {
    //答题情况 0 未完成 1完成
    withdrawStatusFilter(e) {
      switch (e) {
        case 0:
          return "未完成";
          break;
        case 1:
          return "完成";
          break;
        default:
          return "暂无";
          break;
      }
    },
  },
  watch: {
    time1(val) {
      this.params.started_at = val ? val[0] + " " + "-" + " " + val[1] : "";
      console.log(this.params.started_at);
    },
    time2(val) {
      this.params.ended_at = val ? val[0] + " " + "-" + " " + val[1] : "";
      console.log(this.params.ended_at);
    },
  },
  mounted() {
    this.getList(this.params);
  },
  methods: {
    // 按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    //答题活动管理列表
    getList(e) {
      // axios
      //   .get("http://admin3.yuepinyuemei.com/api/admin/annual/index", {
      //     params: {
      //       page: 1,
      //       pagesize: 10
      //     }
      //   })
      //   .then(function(response) {
      //     console.log(response);
      //   })
      //   .catch(function(error) {
      //     console.log(error);
      //   });

      this.tableLoading = true;
      const datas = e;
      getList(this.params)
        .then((res) => {
          // console.log(res);
          if (res.data.code == 200) {
            this.tableData = res.data.data.data;
            this.total = res.data.data.total;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch((error) => {
          this.$message.error(error);
          this.tableLoading = false;
        });
    },
    // 查询
    searchFn() {
      this.params.page = 1;
      this.getList(this.params);
    },
    //导出
    excelEvent() {
      let datas = { ...this.params };
      delete datas.page;
      delete datas.pagesize;
      // axios
      //   .get("http://admin3.yuepinyuemei.com/api/admin/annual/export", {
      //     params: { ...this.params },
      //     responseType: "blob"
      //   })
      //   .then(res => {
      //     exportfile(res);
      //   });
      excelExport(datas).then((res) => {
        exportfile(res);
      });
    },
    // 路由跳转
    toRoutes(e, userId) {
      let route = { name: e, query: { id: userId } };
      this.$router.push(route);
    },
    // 改变页面条数
    handleSizeChange(val) {
      // this.params.pageNum = 1;
      this.params.pagesize = val;
      this.getList(this.params);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.params.page = val;
      this.getList(this.params);
    },
  },
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
/* 表单样式修改 */
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
</style>